﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 小次郎点餐系统.admin;
using 小次郎点餐系统.business;
using 小次郎点餐系统.user;

namespace 小次郎点餐系统
{
    public partial class loginForm : Form
    {
        public loginForm()
        {
            InitializeComponent();
        }

        private void label_new_Click(object sender, EventArgs e)
        {
            zhuceForm form = new zhuceForm();
            form.Show();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            string account = this.txt_account.Text.Trim();
            string pwd = this.txt_password.Text.Trim();
            int type = this.rbtn_user.Checked ? (int)usertype.user : (int)usertype.admin;
            string sql = "";
            if (type == (int)usertype.user)
            {
                sql = string.Format("select * from [user] where account='{0}' and password='{1}'",account,pwd);
            }
            if(type==(int)usertype.admin)
            {
                sql = string.Format("select * from manager where account='{0}' and password='{1}'",account,pwd);
            }
            DataTable table = DBHelper.read(sql);
            if (table.Rows.Count>0)
            { 
                MessageBox.Show("登录成功");
                if (type==(int)usertype.admin)
                {
                    adminMainForm form = new adminMainForm();
                    form.ShowDialog();
                    this.Close();
                }
                else
                {
                    isuser.password = table.Rows[0]["password"].ToString();
                    isuser.phone = table.Rows[0]["phone"].ToString();
                    isuser.account = table.Rows[0]["account"].ToString();
                    isuser.userid = Convert.ToInt32(table.Rows[0]["id"]);
                    userMainForm form = new userMainForm();
                    form.ShowDialog();
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("账号或密码有误请重新登录...");
            }           
        }

        private void label_forget_Click(object sender, EventArgs e)
        {
            forgetForm form = new forgetForm();
            form.Show();
        }

        private void btn_cancer_Click(object sender, EventArgs e)
        {
            foreach (var item in this.Controls)
            {
                if (item is TextBox)
                {
                    TextBox txt = item as TextBox;
                    txt.Text = null;
                }   
            }
        }
    }
}
